package com.yuyou.tas.lesson.service.comm;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuyou.tas.lesson.service.IBaseChapterService;
import com.yuyou.tas.lesson.service.IBaseLevelService;
import com.yuyou.tas.lesson.service.IBaseMaterialService;
import com.yuyou.tas.lesson.service.IBaseSSRService;
import com.yuyou.tas.lesson.service.IBaseSchoolService;
import com.yuyou.tas.lesson.service.IBaseSectionService;
import com.yuyou.tas.lesson.service.IBaseTeaService;
import com.yuyou.tas.lesson.service.IBaseTermService;
import com.yuyou.tas.lesson.service.ILessonChapterService;
import com.yuyou.tas.lesson.service.ILessonMaterialService;
import com.yuyou.tas.lesson.service.ILessonService;
import com.yuyou.tas.service.api.admin.entity.AuthUser;
import com.yuyou.tas.service.api.admin.service.AuthUserAPI;
import com.yuyou.tas.service.api.lesson.entity.BaseChapter;
import com.yuyou.tas.service.api.lesson.entity.BaseLevel;
import com.yuyou.tas.service.api.lesson.entity.BaseMaterial;
import com.yuyou.tas.service.api.lesson.entity.BaseSchool;
import com.yuyou.tas.service.api.lesson.entity.BaseSection;
import com.yuyou.tas.service.api.lesson.entity.BaseSectionSubjectRel;
import com.yuyou.tas.service.api.lesson.entity.BaseTeach;
import com.yuyou.tas.service.api.lesson.entity.BaseTrimester;
import com.yuyou.tas.service.api.lesson.entity.Lesson;
import com.yuyou.tas.service.api.lesson.entity.LessonChapter;
import com.yuyou.tas.service.api.lesson.entity.LessonMaterial;
import com.yuyou.tas.util.enums.OrganLimitEnums;
import com.yuyou.tas.util.message.CommMess;

@Service
public class OrganLimitService {

	@Autowired
	private AuthUserAPI authUserAPI;
	
	@Autowired
	private IBaseChapterService baseChapterService;

	@Autowired
	private ILessonService lessonService;
	
	@Autowired
	private ILessonChapterService lessonChapterService;
	
	@Autowired
	private IBaseLevelService levelService;

	@Autowired
	private IBaseMaterialService baseMaterialService;

	@Autowired
	private IBaseSectionService baseSectionService;

	@Autowired
	private IBaseSSRService ssrService;

	@Autowired
	private IBaseTeaService baseTeaService;

	@Autowired
	private IBaseTermService baseTermService;

	@Autowired
	private ILessonMaterialService lessonMaterialService;

	@Autowired
	private IBaseSchoolService schoolService;



	public void checkOrganByUserId(Long userId, Long targetId,
			OrganLimitEnums.type type) throws Exception {
		AuthUser authUser=authUserAPI.selectById(userId);
		checkOrganByOrganId(authUser.getAuthOrganizationId(),targetId,type);
	}
	
	public void checkOrganByUserId(Long userId, List<Long> targetIds,
			OrganLimitEnums.type type) throws Exception {
		AuthUser authUser=authUserAPI.selectById(userId);
		checkOrganByOrganId(authUser.getAuthOrganizationId(), targetIds,type);
	}
	
	public void checkOrganByOrganId(Long organId, List<Long> targetIds,
			OrganLimitEnums.type type) throws Exception {
//		targetIds.forEach(targetId -> checkOrganByOrganId(organId,targetId,type));
		for(Long targetId:targetIds){
			checkOrganByOrganId(organId,targetId,type);
		}
	}
	
	public void checkOrganByOrganId(Long organId, Long targetId,
			OrganLimitEnums.type type) throws Exception {
		
		switch (type) {
		case base_chapter:
			BaseChapter bm = baseChapterService.selectByPKandOrgan(targetId, organId);
			if(bm == null){
				throw new Exception(CommMess.ERR_POWER);
			}
			break;
		case base_level:
			BaseLevel bl = levelService.selectByPKandOrgan(targetId, organId);
			if(bl == null){
				throw new Exception(CommMess.ERR_POWER);
			}
			break;
		case base_material:
			BaseMaterial baseMaterial = baseMaterialService.selectByPKandOrgan(targetId , organId);
			if(baseMaterial == null){
				throw new Exception(CommMess.ERR_POWER);
			}
			break;
		case base_school:
			BaseSchool baseSchool = schoolService.selectByPKandOrgan(targetId , organId);
			if(baseSchool == null){
				throw new Exception(CommMess.ERR_POWER);
			}
			break;
//		case base_section:
//			BaseSection baseSection = baseSectionService.selectByPKandOrgan(targetId , organId);
//			if(baseSection == null){
//				throw new Exception(CommMess.ERR_POWER);
//			}
//			break;
		case base_teach:
			BaseTeach baseTeach = baseTeaService.selectByPKandOrgan(targetId , organId);
			if(baseTeach == null){
				throw new Exception(CommMess.ERR_POWER);
			}
			break;
		case base_trimester:
			BaseTrimester baseTrimester = baseTermService.selectByPKandOrgan(targetId , organId);
			if(baseTrimester == null){
				throw new Exception(CommMess.ERR_POWER);
			}
			break;
		case lesson:
			Lesson Lesson=lessonService.selectByPKandOrgan(targetId, organId);
			if(Lesson==null){
				throw new Exception(CommMess.ERR_POWER);
			}
			break;
		case lesson_chapter:
			LessonChapter lessonChapter=lessonChapterService.selectByPKandOrgan(targetId, organId);
			if(lessonChapter==null){
				throw new Exception(CommMess.ERR_POWER);
			}
			break;
		case lesson_class:
			break;
		case lesson_class_plan:
			break;
		case lesson_material:
			LessonMaterial lessonMaterial = lessonMaterialService.selectByPKandOrgan(targetId, organId);
			if(lessonMaterial == null){
				throw new Exception(CommMess.ERR_POWER);
			}
			break;
		default:
			break;
		}
	}

}
